<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="java.sql.*"%>
<%@page language="java" import="java.util.*"%>



<%
            String role = request.getParameter("role");
            String username = request.getParameter("username");
            String password = request.getParameter("password");

            Connection connection = null;
            try {

                String driverName = "oracle.jdbc.driver.OracleDriver";
                Class.forName(driverName);

                // Create a connection to the database
                String serverName = "10.0.0.27";
                String portNumber = "1521";
                String sid = "XE";
                String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
                String uname = "aranya27";
                String pword = "doodledo";

                connection = DriverManager.getConnection(url, uname, pword);



              // enter code here

                Statement st = null;
                out.println(role);
                st = connection.createStatement();
                if(role.equals("student"))
                {
                String str = "select * from aranya27.student_ranks where rank=" + username;
                ResultSet rs = st.executeQuery(str);

               // ResultSet rs1 = st.executeQuery(str);
                if(!rs.next()){response.sendRedirect("login.jsp?err=1");
                    }
               else {
                    String passwd = rs.getString("password");
                    if (passwd.equals(password)) {
                        session.setAttribute("role", role);
                        session.setAttribute("suid", rs.getString("suid"));
                        session.setAttribute("password", rs.getString("password"));
                         session.setAttribute("rank",rs.getString("rank"));
                        session.setAttribute("category",rs.getString("category"));
                        String str1 = "select * from ARANYA27.TEST_ALLOT where suid=" + rs.getString("suid");
                        ResultSet rs1 = st.executeQuery(str1);
                        if(rs1.next())
                        {
                            if(rs1.getString("flag").equals("0"))
                                response.sendRedirect("student/studentDetails.jsp");
                            else if(rs1.getString("flag").equals("1"))
                                response.sendRedirect("student/studentHome.jsp");
                            else if(rs1.getString("flag").equals("2"))
                                response.sendRedirect("student/studentHome.jsp");
                        }
                    } else {
                        response.sendRedirect("login.jsp?err=1");
                    }
                }
                }
                else if(role.equals("admin"))
                {
                    out.println(username);
                    String str = "select * from aranya27.admin_details where ename='" + username+"'";
                    out.println(str);
                ResultSet rs = st.executeQuery(str);out.println("XXXXXXXXX");
                if(!rs.next()){response.sendRedirect("login.jsp?err=1");
                   }
                else {
                    out.println("LLLLL");
                    String passwd = rs.getString("password");
                    if (passwd.equals(password)) {
                        session.setAttribute("role", role);
                        session.setAttribute("password", rs.getString("password"));
                        session.setAttribute("suid", rs.getString("eid"));
                         session.setAttribute("ename",rs.getString("ename"));
                         response.sendRedirect("admin/tasks.jsp");
                    } else {
                        response.sendRedirect("login.jsp?err=1");
                    }
                }
                }
                else
                    {out.println("I dont know whtat is happening");}

                connection.close();


                //end here

            } catch (ClassNotFoundException e) {
                e.printStackTrace();
                System.out.println(" Could not find the database driver");
            } catch (SQLException e) {
                e.printStackTrace();
                System.out.println("Could not connect to the database");
            }



%>